package com.apes.fn.tms.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.fn.tms.model.WaybillPlanningLog;
import com.apes.fn.tms.repository.WaybillPlanningLogRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.*;

@Service("waybillPlanningLogService")
public class WaybillPlanningLogService {
    @Autowired
    private WaybillPlanningLogRepository waybillPlanningLogRepository;
    @Autowired
    private EntityManager entityManager;

    public void create(SimpleRequest request) {
        WaybillPlanningLog waybillPlanningLog = request.getO(WaybillPlanningLog.class);
        waybillPlanningLog.setCreateDate(new Date());
        waybillPlanningLogRepository.save(waybillPlanningLog);
    }


    public JSONObject qryWaybillPlanningLog(SimpleRequest request) {
        JSONObject getJO = request.getJO();
        String waybillId = getJO.getString("WAYBILL_ID");
        List<Map> mainWaybillPlanningLog = waybillPlanningLogRepository.findMainWaybillPlanningLog(waybillId);
        JSONObject returnObj = new JSONObject();
        if (!mainWaybillPlanningLog.isEmpty()){
            returnObj.putAll(mainWaybillPlanningLog.get(0));
        }
        List<Map> waybillPlanningLog = waybillPlanningLogRepository.findWaybillPlanningLog(waybillId);
        String carInfomationId = "";
        JSONArray sum_sumSameWaybillPlanningLog_Array = new JSONArray();
        JSONObject sumSameWaybillPlanningLog_Obj = new JSONObject(); // 汇总
        JSONArray waybillPlanningLog_Array = new JSONArray(); // 多条明细
        JSONObject waybillPlanningLog_Obj = new JSONObject(); // 一个对象
        for (Map row:waybillPlanningLog) {
            String rCarInfomationId = row.get("CAR_INFORMATION_ID") == null?"":row.get("CAR_INFORMATION_ID").toString();
            if ("".equals(carInfomationId) || carInfomationId.contains("," + rCarInfomationId + ",")) {
                waybillPlanningLog_Obj.putAll(row);
                waybillPlanningLog_Array.add(waybillPlanningLog_Obj);
                if (!carInfomationId.contains("," + rCarInfomationId + ",")) {
                    sumSameWaybillPlanningLog_Obj.put("CAR_INFORMATION_ID", rCarInfomationId);
                    carInfomationId += "," + rCarInfomationId + ",";
                }
                waybillPlanningLog_Obj = new JSONObject();
            } else {
                sumSameWaybillPlanningLog_Obj.put("ITEM", waybillPlanningLog_Array);
                sum_sumSameWaybillPlanningLog_Array.add(sumSameWaybillPlanningLog_Obj);
                sumSameWaybillPlanningLog_Obj = new JSONObject();
                waybillPlanningLog_Array = new JSONArray();
                waybillPlanningLog_Obj = new JSONObject();
                waybillPlanningLog_Obj.putAll(row);
                sumSameWaybillPlanningLog_Obj.put("CAR_INFORMATION_ID", rCarInfomationId);
                carInfomationId += "," + rCarInfomationId + ",";
            }
            if (waybillPlanningLog.size() == 1) {
                sumSameWaybillPlanningLog_Obj.put("ITEM", waybillPlanningLog_Array);
                sum_sumSameWaybillPlanningLog_Array.add(sumSameWaybillPlanningLog_Obj);
                waybillPlanningLog_Array = new JSONArray();
            }
        }
        if (waybillPlanningLog_Array.size() > 0) {
            sumSameWaybillPlanningLog_Obj.put("ITEM", waybillPlanningLog_Array);
            sum_sumSameWaybillPlanningLog_Array.add(sumSameWaybillPlanningLog_Obj);
        }
        returnObj.put("DATA", sum_sumSameWaybillPlanningLog_Array);
        return returnObj;
    }
}
